home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / ADDHNDLS.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  3KB  |  105 lines

  1. /*
  2. **  ADDHNDLS.C
  3. **
  4. **  A compilation of public domain sources originally written by
  5. **  Doug Burger and Bob Jarvis
  6. **
  7. **  Collected and modified for Zortech, Microsoft, and Borland by Bob Stout
  8. **
  9. **  Demonstrates relocating the file handle table under DOS 3.x
  10. **  for having more than the usual 20 files open in a single
  11. **  program
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <dos.h>
  16. #include <io.h>
  17. #include <fcntl.h>
  18.  
  19. #define TABLE_SIZE 255        /* NOTE: *Must* be <= FILES in CONFIG.SYS */
  20.  
  21. #ifdef TEST
  22.  #if !defined(__ZTC__) && !defined(__TURBOC__)  /* i.e. #if MSC/QC      */
  23.   #include <stdlib.h>
  24.   #define MK_FP(seg,offset) \
  25.          ((void far *)(((unsigned long)(seg)<<16) | (unsigned)(offset)))
  26.  
  27.   /* MSC's open() is funny - this code only works with _dos_open()      */
  28.  
  29.   int open(const char *name, int mode, ...)
  30.   {
  31.           int hdl;
  32.  
  33.           if (0 == _dos_open(name, mode, &hdl))
  34.                   return hdl;
  35.           else    return -1;
  36.   }
  37.  #endif /* MSC */
  38. #endif /* TEST */
  39.  
  40. unsigned char handle_table[TABLE_SIZE];     /* table of file DOS handles    */
  41. unsigned char far * far * handle_ptr;       /* ptr to DOS's ptr to hand.    */
  42. unsigned int far *handle_count;             /* ptr to handle count          */
  43.  
  44. int relocate(void)
  45. {
  46.     switch (_osmajor)
  47.     {
  48.     case 2:
  49.         return -1;
  50.     case 3:
  51.         if (3 > _osminor)
  52.         {                                       /* by Doug Burger           */
  53.             unsigned int i;
  54.  
  55.             handle_count = MK_FP(_psp, 0x32);   /* handle count at PSP:32h  */
  56.             handle_ptr = MK_FP(_psp, 0x34);     /* table ptr at PSP:34h     */
  57.             for (i = 0; i < *handle_count; i++) /* relocate exiting table   */
  58.                 handle_table[i] = (*handle_ptr)[i];
  59.             for (i = *handle_count; i < TABLE_SIZE; i++)    /* init. rest   */
  60.                 handle_table[i] = 255;
  61.             *handle_ptr = handle_table;         /* set pointer to new table */
  62.             *handle_count = TABLE_SIZE;         /* set new table size       */
  63.             return 0;
  64.         }
  65.         else
  66.     default:                                    /* DOS 4+                   */
  67.         {                                       /* by Bob Jarvis            */
  68.             union REGS regs;
  69.  
  70.             regs.h.ah = 0x67;
  71.             regs.x.bx = TABLE_SIZE | 1;         /* has to be an odd number  */
  72.  
  73.             intdos(®s, ®s);
  74.  
  75.             if(regs.x.cflag)                    /* error                    */
  76.                 return -1;
  77.             else
  78.                 return 0;
  79.         }
  80.     }
  81. }   /*  relocate()  */
  82.  
  83. /*
  84. **  Test code
  85. */
  86.  
  87. #ifdef TEST
  88.  
  89. void main(void)
  90. {
  91.     int c, h;
  92.  
  93.     relocate();
  94.  
  95.     c = 0;
  96.     while ((h = open("CON", O_RDONLY)) >= 0)        /* DOS closes files */
  97.     {
  98.         c++;                                        /* on exit, so I    */
  99.         printf("handle = %d\n", h);                 /* don't bother     */
  100.     }                                               /* saving handles   */
  101.     printf("total opened files = %d\n", c);
  102. }   /*  ADDHNDLS.C  */
  103.  
  104. #endif /* TEST */
  105.